!include "C:\Program Files\NSIS\Include\defines.nsh"

Name "Forged Alliance livereplay configurator"
OutFile LivereplayConfig.exe
 
ShowInstDetails show
XPStyle on

Page instfiles

Function FindFiles
  Exch $R5 # callback function
  Exch 
  Exch $R4 # file name
  Exch 2
  Exch $R0 # directory
  Push $R1
  Push $R2
  Push $R3
  Push $R6
 
  Push $R0 # first dir to search
 
  StrCpy $R3 1
 
  nextDir:
    Pop $R0
    IntOp $R3 $R3 - 1
    ClearErrors
    FindFirst $R1 $R2 "$R0\*.*"
    nextFile:
      StrCmp $R2 "." gotoNextFile
      StrCmp $R2 ".." gotoNextFile
 
      StrCmp $R2 $R4 0 isDir
        Push "$R0\$R2"
        Call $R5
        Pop $R6
        StrCmp $R6 "stop" 0 isDir
          loop:
            StrCmp $R3 0 done
            Pop $R0
            IntOp $R3 $R3 - 1
            Goto loop
 
      isDir:
        IfFileExists "$R0\$R2\*.*" 0 gotoNextFile
          IntOp $R3 $R3 + 1
          Push "$R0\$R2"
 
  gotoNextFile:
    FindNext $R1 $R2
    IfErrors 0 nextFile
 
  done:
    FindClose $R1
    StrCmp $R3 0 0 nextDir
 
  Pop $R6
  Pop $R3
  Pop $R2
  Pop $R1
  Pop $R0
  Pop $R5
  Pop $R4
FunctionEnd
 
!macro CallFindFiles DIR FILE CBFUNC
Push "${DIR}"
Push "${FILE}"
Push $0
GetFunctionAddress $0 "${CBFUNC}"
Exch $0
Call FindFiles
!macroend
 
Function FindForgedAlliance
  Pop $0
  GetDLLVersion $0 $1 $3
  IntOp $2 $1 & 0x0000FFFF
  IntOp $1 $1 / 0x00010000
  IntOp $4 $3 & 0x0000FFFF
  IntOp $3 $3 / 0x00010000
  DetailPrint "$1.$2.$3.$4 - $0"
  StrCmp "$1.$2" "1.5" 0 not
    StrCpy $9 $0
    DetailPrint "Executable file found. Stopping search..."
    Push "stop"
    Return
  not:
    Push "go"
FunctionEnd
 
Section "Find ForgedAlliance"
	DetailPrint "Looking for executable path in the registry..."
	ReadRegStr $9 HKCU "Software\THQ\Gas Powered Games\Supreme Commander - Forged Alliance" "InstallationDirectory"
	StrCmp $9 "" findfile 0
	
	StrCpy $9 "$9\Supreme Commander - Forged Alliance\bin\ForgedAlliance.exe"
	DetailPrint "ForgedAlliance found at at: $9"
	Goto writeRegistry

	findfile:
		!insertmacro CallFindFiles "C:\Program Files" "ForgedAlliance.exe" FindForgedAlliance
		StrCmp $9 "" 0 writeRegistry
			DetailPrint "Forged Alliance executable file not found in Program Files"


	askFolder:
		DetailPrint "Manual selection of Forged Alliance executable file..."
		Dialogs::Open "Forged Alliance executable (ForgedAlliance.exe)|ForgedAlliance.exe" "4" "Please select the Forged Alliance executable file" "C:" ${VAR_6}
		StrCmp $6 "0" Cancel Ok

		Cancel:
			DetailPrint "Couldn't find Forged Alliance on this computer"
			Goto exit

		Ok:
			DetailPrint "Selected file: $6"
			StrCpy $9 $6

	
	writeRegistry:
	; Prepare the string to be written in the registry	
	StrCpy $9 "$\"$9$\"  /replay %1"
		
	DetailPrint "Writing necessary information in the registry..."
	;WriteRegStr HKCR "gpgnet" "" "URL:gpgnet Protocol"
	;WriteRegStr HKCR "gpgnet" "URL Protocol" ""
	;WriteRegStr HKCR "gpgnet\shell\open\command" "" "$9"
	
	WriteRegStr HKCU "Software\Classes\gpgnet" "" "URL:gpgnet Protocol"
	WriteRegStr HKCU "Software\Classes\gpgnet" "URL Protocol" ""
	WriteRegStr HKCU "Software\Classes\gpgnet\shell\open\command" "" "$9"
	exit:
	
SectionEnd